<template>
  <!-- 评教管理 -->
  <div>
    <div class="edu-page-warp">
      <condition-card @reset="reset" @search="searchAll" :label-width="labelWidth" style="margin-bottom: 20px;">
        <el-row :gutter="20">
          <!-- 开课学院 -->
          <el-col :span="8">
            <el-form-item :label="$t('label.openClassCollege')">
              <DictSelect
                labelAll="请选择"
                :disabled="facultyDisable"
                filterable multiple collapse-tags
                v-model="queryParams.facultyList"
                :data="options.faculty"
                @change="handleChangeFaculty"
              ></DictSelect>
            </el-form-item>
          </el-col>
          <!-- 考核方式 -->
          <el-col :span="8">
            <el-form-item :label="$t('label.assessmentMode')">
              <DictSelect
                v-model="queryParams.assessmentModeList"
                :data="options.assessmentMode"
                clearable
                filterable
                multiple
                collapse-tags>
              </DictSelect>
            </el-form-item>
          </el-col>
          <!-- 课程性质 -->
          <el-col :span="8">
            <el-form-item :label="$t('courseSummary.nature')" prop="courseCategory">
              <dict-select
                v-model="queryParams.natureList"
                :data="options.courseNatureList"
                :show-all="false"
                clearable
                filterable
                multiple
                collapse-tags
              />
            </el-form-item>
          </el-col>
          <!-- 授课语言 -->
          <el-col :span="8">
            <el-form-item :label="$t('授课语言')">
              <el-select v-model="queryParams.teachingLanguageList"
                clearable
                filterable
                multiple collapse-tags style="width: 100%;">
                <el-option
									v-for="item in options.teachingLanguage"
									:key="item.value"
									:label="item.label"
									:value="item.value"
								></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 课程名称 -->
          <el-col :span="8">
            <el-form-item :label="$t('课程名称')">
              <el-input v-model="queryParams.courseName" clearable :placeholder="$t('请输入')"></el-input>
            </el-form-item>
          </el-col>
          <!-- 课程层次 -->
          <el-col :span="8">
            <el-form-item  :label="$t('课程层次')">
              <DictSelect
                  v-model="queryParams.trainingLevelList"
                  :data="options.stuTrainingLevel"
                  clearable
                  filterable
                  multiple
                  collapse-tags
              ></DictSelect>
            </el-form-item>
          </el-col>
          <!-- 课程代码 -->
          <el-col :span="8">
            <el-form-item :label="$t('课程代码')">
              <el-input v-model="queryParams.courseCode" clearable :placeholder="$t('请输入')"></el-input>
            </el-form-item>
          </el-col>
          <!-- 授课教师区分 -->
          <el-col :span="8">
            <el-form-item :label="$t('授课教师区分')">
              <el-select v-model="queryParams.firstTeacherStatus" clearable style="width: 100%;">
                <el-option
                  v-for="item in teacherTypeList"
                  :key="item.code"
                  :label="item.teacherTypeName"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>

      <el-card shadow="never">
        <div class="edu-opertion-bar">
          <div class="float-left edu-page-title">{{titleDone}}</div>
          <div class="float-right">
            <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
              <el-button type="danger" @click="deleteObjectClick">{{$t('移除')}}</el-button>
            </el-row>
          </div>
        </div>
        <!-- 主页表格 -->
        <edu-table :url="doneUrl" ref="doneTable" :order="true" :selection="true">
          <edu-table-column :label="$t('开课学院')" prop="facultyName"></edu-table-column>
          <edu-table-column :label="$t('课程性质')" prop="natureName"></edu-table-column>
          <edu-table-column :label="$t('考核方式')" prop="assessmentModeName"></edu-table-column>
          <edu-table-column :label="$t('授课语言')" prop="teachingLanguageName"></edu-table-column>
          <edu-table-column :label="$t('课程层次')" prop="trainingLevelName"></edu-table-column>
          <edu-table-column :label="$t('课程名称')" prop="courseName"></edu-table-column>
          <edu-table-column :label="$t('课程代码')" prop="courseCode"></edu-table-column>
          <edu-table-column :label="$t('教学班代码')" prop="teachingClassId"></edu-table-column>
          <edu-table-column :label="$t('第一任课教师')" prop="firstTeacherNameCode"></edu-table-column>
          <edu-table-column :label="$t('其他任课教师')" prop="otherTeacherNameCode"></edu-table-column>
          <edu-table-column :label="$t('班级人数')" prop="studentNumber"></edu-table-column>
        </edu-table>
      </el-card>

      <!-- 表格card -->
      <el-card shadow="never">
        <div class="edu-opertion-bar">
          <div class="float-left edu-page-title">{{titleNot}}</div>
          <div class="float-right">
            <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
              <el-button type="primary" @click="save">{{$t('添加')}}</el-button>
            </el-row>
          </div>
        </div>
        <!-- 主页表格 -->
        <edu-table :url="url" ref="mainTable" :order="true" :selection="true">
          <edu-table-column :label="$t('开课学院')" prop="facultyName"></edu-table-column>
          <edu-table-column :label="$t('课程性质')" prop="natureName"></edu-table-column>
          <edu-table-column :label="$t('考核方式')" prop="assessmentModeName"></edu-table-column>
          <edu-table-column :label="$t('授课语言')" prop="teachingLanguageName"></edu-table-column>
          <edu-table-column :label="$t('课程层次')" prop="trainingLevelName"></edu-table-column>
          <edu-table-column :label="$t('课程名称')" prop="courseName"></edu-table-column>
          <edu-table-column :label="$t('课程代码')" prop="courseCode"></edu-table-column>
          <edu-table-column :label="$t('教学班代码')" prop="teachingClassId"></edu-table-column>
          <edu-table-column :label="$t('第一任课教师')" prop="firstTeacherNameCode"></edu-table-column>
          <edu-table-column :label="$t('其他任课教师')" prop="otherTeacherNameCode"></edu-table-column>
          <edu-table-column :label="$t('班级人数')" prop="studentNumber"></edu-table-column>
        </edu-table>
        <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
          <el-button type="primary" @click="prevStep">{{$t('上一步')}}</el-button>
          <el-button @click="cancel">{{$t('返回')}}</el-button>
        </el-row>
      </el-card>
    </div>
  </div>
</template>
<script>
import UTILS from "common/src/utils/utils";
import baseUrl from 'common/src/api/teachingEvaluation/env'
import { queryDic } from "common/src/api/dictionary"
import courseEvaluationApi from 'common/src/api/teachingEvaluation/courseEvaluation'
export default {
  props: {
    ifEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      routeParams: {},
      titleNot: '未配置对象列表',
      titleDone: '已配置对象列表',
      url: `${baseUrl}/studentManagement/object/getPageList`,
      doneUrl: `${baseUrl}/studentManagement/object/look/getPageList`,
      labelWidth: '120px',
      exportButton: false,
      exportResult: null,
      currentCalendarId: '',
      queryParams: {
        calendarId: '',
        facultyList: [],
        assessmentModeList: [],
        natureList: [],
        teachingLanguageList: [],
        courseName: '',
        trainingLevelList: [],
        courseCode: '',
        firstTeacherStatus: ''
      },
      options: {
        courseNatureList: [], //课程性质
        trainingLevelList: [], //培养层次
        assessmentMode: [], // 考核方式
        stuTrainingLevel: [], //课程层次
        teachingLanguage: [], //授课语言
        faculty: []
      },
      teacherTypeList: [
        {
          teacherTypeName: '第一任课教师',
          code: '1'
        },
        {
          teacherTypeName: '其他任课教师',
          code: '0'
        }
      ],
      tableData: [],
      facultyList: [],
      dataList: [],
      facultyDisable: false
    }
  },
  created() {
    if (sessionStorage.getItem('currFacultyCode')) {
      this.queryParams.facultyList.push(sessionStorage.getItem('currFacultyCode'))
      this.facultyDisable = true
    }
    this.getOptions();
  },
  mounted() {
    if (this.$route.query.managementId) {
      this.queryParams.managementId = this.$route.query.managementId;
      this.queryParams.calendarId = this.$route.query.calendarId;
      this.searchAll()
    }
  },
  methods: {
    // 查询两个表格数据
    searchAll() {
      this.searchList()
      this.searchDoneList()
    },
    // 查询
    searchList() {
      this.$refs.mainTable.pageNum = 1;
      this.queryParams.courseCode = this.queryParams.courseCode.trim()
      this.queryParams.courseName = this.queryParams.courseName.trim()
      this.$refs.mainTable.search(this.queryParams)
    },
    // 查询
    searchDoneList() {
      this.$refs.doneTable.pageNum = 1;
      this.queryParams.courseCode = this.queryParams.courseCode.trim()
      this.queryParams.courseName = this.queryParams.courseName.trim()
      this.$refs.doneTable.search(this.queryParams)
    },
    // 重置查询参数
    reset() {
      this.queryParams = {
        managementId: this.$route.query.managementId ? this.$route.query.managementId : this.routeParams.managementId,
        calendarId: this.$route.query.calendarId ? this.$route.query.calendarId : this.routeParams.calendarId,
        facultyList: [],
        assessmentModeList: [],
        natureList: [],
        teachingLanguageList: [],
        courseName: '',
        trainingLevelList: [],
        courseCode: '',
        firstTeacherStatus: ''
      };
      if (this.facultyDisable) {
        this.queryParams.facultyList.push(sessionStorage.getItem('currFacultyCode'))
      }
      this.searchAll();
    },
    // 获取字典数据
    async getOptions() {
      //筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: ["X_KCXZ", "X_PYCC", "X_KSLX", "X_PYCC", "X_SKYY", "X_YX"]
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            courseNatureList: "X_KCXZ", // 课程性质
            trainingLevelList: "X_PYCC", // 培养层次
            assessmentMode: "X_KSLX", // 考核方式
            stuTrainingLevel: "X_PYCC", // 课程层次
            teachingLanguage: "X_SKYY", // 授课语言
            faculty: "X_YX"
          },
          res.data,
          this.options
        );
      });
    },
    // 取消 直接返回列表页
    cancel() {
      this.$emit('onlyGoBack')
      this.$router.push({
        query: {}
      })
    },
    // 上一步 返回上一页
    prevStep() {
      if (this.ifEdit) {
        this.$emit('editGoBack')
      } else {
        this.$emit('goToAddOrEdit')
      }
    },
    // 对象配置最后一次保存
    save() {
      const checkDatas = this.$refs.mainTable.getCheckData();
      let param = Object.assign({},this.queryParams)
      param.objectList = JSON.parse(JSON.stringify(checkDatas))
      courseEvaluationApi.objectStudentEvaSetConfirm(param).then(res => {
        if (res.code == 200) {
          this.$message.success(this.$t('新增评教对象配置成功，请稍后发布'))
          this.searchAll()
        }
      })
    },
    // 删除对象配置
    deleteObjectClick() {
      this.$confirm(this.$t('确定要移除已配置的这些对象吗？'), this.$t("message.prompt"), {
        confirmButtonText: this.$t("message.confirm"),
        cancelButtonText: this.$t("message.cancel"),
        type: "warning"
      }).then(() => {
        const checkDatas = this.$refs.doneTable.getCheckData();
        let param = Object.assign({},this.queryParams)
        param.objectList = JSON.parse(JSON.stringify(checkDatas))
        courseEvaluationApi.objectStudentEvaSetDelete(param).then(res => {
          if (res.code == 200) {
            this.$message.success(this.$t('对象删除成功，请稍后发布'))
            this.searchAll()
          }
        })
      })
    }
  },
  watch: {
    '$route'(to, from) {
        if (Object.values(to.query).length > 0) {
          this.routeParams = _.cloneDeep(to.query)
          if (this.routeParams.managementId) {
            this.queryParams.managementId = this.routeParams.managementId;
            this.queryParams.calendarId = this.routeParams.calendarId;
            this.searchAll()
          }
        }
    }
  }
}
</script>
<style lang="scss">
</style>
